]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
telepathyClient: Use proper Object to wrap different tpl messages
authorMarco Trevisan (Treviño) <mail@3v1n0.net>
Fri, 20 Mar 2020 19:31:45 +0000 (20:31 +0100)
committerFlorian Müllner <florian.muellner@gmail.com>
Wed, 25 Mar 2020 22:22:49 +0000 (22:22 +0000)
In telepathyClient we consider messages both Tpl.TextEvents and
Tpl.Messages, and we manually create JS objects to copy the properties we
care for each one. This may lead to objects not matching the interface we
want.

Instead, use an object with construct-only properties and two factory static
methods to initialize it.

Unfortunately we need to use the ChatMessageClass for the class name or
calling the static methods would trigger a gjs error as per [1].

https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1113

[1] https://gitlab.gnome.org/GNOME/gjs/-/issues/310

js/ui/components/telepathyClient.js

index cdb7f57f5add0d3301e573f69cf4e64038537e5e..5a65e84e8ba6f28d9e50c6f50306b7d657d8a5fa 100644 (file)
@@ -39,35 +39,58 @@ var NotificationDirection = {
     RECEIVED: 'chat-received',
 };
 
-function makeMessageFromTpMessage(tpMessage, direction) {
-    let [text, flags_] = tpMessage.to_text();
-
-    let timestamp = tpMessage.get_sent_timestamp();
-    if (timestamp == 0)
-        timestamp = tpMessage.get_received_timestamp();
-
-    return {
-        messageType: tpMessage.get_message_type(),
-        text,
-        sender: tpMessage.sender.alias,
-        timestamp,
-        direction,
-    };
-}
+const ChatMessage = HAVE_TP ? GObject.registerClass({
+    Properties: {
+        'message-type': GObject.ParamSpec.int(
+            'message-type', 'message-type', 'message-type',
+            GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
+            Math.min(...Object.values(Tp.ChannelTextMessageType)),
+            Math.max(...Object.values(Tp.ChannelTextMessageType)),
+            Tp.ChannelTextMessageType.NORMAL),
+        'text': GObject.ParamSpec.string(
+            'text', 'text', 'text',
+            GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
+            null),
+        'sender': GObject.ParamSpec.string(
+            'sender', 'sender', 'sender',
+            GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
+            null),
+        'timestamp': GObject.ParamSpec.int64(
+            'timestamp', 'timestamp', 'timestamp',
+            GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
+            0, Number.MAX_SAFE_INTEGER, 0),
+        'direction': GObject.ParamSpec.string(
+            'direction', 'direction', 'direction',
+            GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
+            null),
+    },
+}, class ChatMessageClass extends GObject.Object {
+    static newFromTpMessage(tpMessage, direction) {
+        return new ChatMessage({
+            'message-type': tpMessage.get_message_type(),
+            'text': tpMessage.to_text()[0],
+            'sender': tpMessage.sender.alias,
+            'timestamp': direction === NotificationDirection.RECEIVED
+                ? tpMessage.get_received_timestamp() : tpMessage.get_sent_timestamp(),
+            direction,
+        });
+    }
 
+    static newFromTplTextEvent(tplTextEvent) {
+        let direction =
+            tplTextEvent.get_sender().get_entity_type() === Tpl.EntityType.SELF
+                ? NotificationDirection.SENT : NotificationDirection.RECEIVED;
 
-function makeMessageFromTplEvent(event) {
-    let sent = event.get_sender().get_entity_type() == Tpl.EntityType.SELF;
-    let direction = sent ? NotificationDirection.SENT : NotificationDirection.RECEIVED;
+        return new ChatMessage({
+            'message-type': tplTextEvent.get_message_type(),
+            'text': tplTextEvent.get_message(),
+            'sender': tplTextEvent.get_sender().get_alias(),
+            'timestamp': tplTextEvent.get_timestamp(),
+            direction,
+        });
+    }
+}) : null;
 
-    return {
-        messageType: event.get_message_type(),
-        text: event.get_message(),
-        sender: event.get_sender().get_alias(),
-        timestamp: event.get_timestamp(),
-        direction,
-    };
-}
 
 var TelepathyComponent = class {
     constructor() {
@@ -430,7 +453,7 @@ class ChatSource extends MessageTray.Source {
     _displayPendingMessages(logManager, result) {
         let [success_, events] = logManager.get_filtered_events_finish(result);
 
-        let logMessages = events.map(makeMessageFromTplEvent);
+        let logMessages = events.map(e => ChatMessage.newFromTplTextEvent(e));
         this._ensureNotification();
 
         let pendingTpMessages = this._channel.get_pending_messages();
@@ -442,7 +465,8 @@ class ChatSource extends MessageTray.Source {
             if (message.get_message_type() == Tp.ChannelTextMessageType.DELIVERY_REPORT)
                 continue;
 
-            pendingMessages.push(makeMessageFromTpMessage(message, NotificationDirection.RECEIVED));
+            pendingMessages.push(ChatMessage.newFromTpMessage(message,
+                NotificationDirection.RECEIVED));
 
             this._pendingMessages.push(message);
         }
@@ -540,7 +564,8 @@ class ChatSource extends MessageTray.Source {
         this._pendingMessages.push(message);
         this.countUpdated();
 
-        message = makeMessageFromTpMessage(message, NotificationDirection.RECEIVED);
+        message = ChatMessage.newFromTpMessage(message,
+            NotificationDirection.RECEIVED);
         this._notification.appendMessage(message);
 
         // Wait a bit before notifying for the received message, a handler
@@ -565,7 +590,8 @@ class ChatSource extends MessageTray.Source {
     // our client and other clients as well.
     _messageSent(channel, message, _flags, _token) {
         this._ensureNotification();
-        message = makeMessageFromTpMessage(message, NotificationDirection.SENT);
+        message = ChatMessage.newFromTpMessage(message,
+            NotificationDirection.SENT);
         this._notification.appendMessage(message);
     }